<?php
/**
 * 直播关联用户
 * @author ASUS
 *
 */
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class user extends admin{
	private $db;
	public $siteid;
	public $admin_userid;
	
	function __construct(){
		parent::__construct();
		pc_base::load_app_func('global');
		$this->db = pc_base::load_model('subject_user_model'); // 加载直播用户关联model
		$this->user_db = pc_base::load_model('user_info_model');//加载用户model
		$this->siteid = get_siteid(); // 获取当前站点ID
		$this->admin_userid = param::get_cookie('userid');//获取当前登录的管理员ID
	}
	
	public function bindView(){
		$subject_id= $_REQUEST['subject_id'];
		$user_array=$this->db->select(array("subject_live_id"=>$subject_id),"user_id");
		$where=" status=1";
		if(!empty($user_array)){
			foreach ($user_array as $k => $v) {
				$id[] = intval($v['user_id']);
			}
			$id = implode('\',\'', $id);
			$where.=" and id not in ('$id')";
		}
		// 搜索框
		$keywords = isset($_GET['keywords']) ? $_GET['keywords'] : '';
		$tel = isset($_GET['tel']) ? $_GET['tel'] : '';
		
		if (isset($_GET['search'])) {
			if ($keywords) {
				$where .= " and `nickname` LIKE '%$keywords%'";
			}
			if ($tel) {
				$where .= " and tel =".$tel;
			}
		}
		
		//获取用户管理员真实姓名数据
		$admin_model = pc_base::load_model('admin_model');
		$adminInfo = $admin_model->getRealnameInfo();

		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$infos = $this->user_db->listinfo($where,'id DESC',$page,10);
		$pages = $this->user_db->pages;
		include $this->admin_tpl('subject_user_select');
	}
	
	public function  bindAction(){
		$subject_id= $_REQUEST['subject_id'];
		$relation=$_REQUEST['relation'];
		$source=explode("|",$relation);
		$data=array();
		
		foreach($source as $k=>$v){
			
			$data[$k]['subject_live_id']=$subject_id;
			$data[$k]['user_id']=$v;
			$data[$k]['created_by'] =  $_SESSION['userid'];		
			$data[$k]['create_time'] = date("Y-m-d H:i:s");
		}
		$user_id = implode(',', $source);//关联的用户ID

		$res=$this->db->multiInsert($data);
		if($res){
			//记录行为日志
			$log_content = '<span style="color:red;">直播关联用户</span>[用户id：'.$user_id.']';
			$this->manage_admin_log($log_content);
			echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
		}else{
			echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
		}
	}
	
	/**
	 * 删除直播关联用户
	 */
	public function del(){
		$sid = $_REQUEST['subject_id'];//直播ID
		$del_uid = $uid = $_REQUEST['uid'];//用户ID
		$id = $_REQUEST['id'];//直播关联用户自增ID
		if (!isset($id) || empty($id)) {
			showmessage(L('message_not_exits'),HTTP_REFERER);
		}

		$result = $this->db->delete("id in ('$id') and subject_live_id=".$sid);
		if ($result){
			//记录行为日志
			$log_content = '<span style="color:red;">删除直播关联用户</span>[用户id：'.$del_uid.']';
			$this->manage_admin_log($log_content);
			showmessage(L('del_success'),'?m=subject_live&c=manage&a=user_list&id='.$sid);
		} else {
			showmessage(L('del_error'),HTTP_REFERER);
		}
	}
	
	/**
	 * 批量删除直播关联用户
	 */
	public function del_all(){
		$sid = $_REQUEST['subject_id'];//直播ID
		$relation=$_REQUEST['relation'];//用户ID
		$source=explode("|",$relation);
		$id = $_REQUEST['id'];//直播关联用户自增ID
		if (!isset($id) || empty($id)) {
			showmessage(L('message_not_exits'),HTTP_REFERER);
		}
		if (is_array($id)){
			foreach ($id as $k=>$v){
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		if (is_array($source)) {
			$del_uid = implode(',', $source);
		}

		$result = $this->db->delete("id in ('$id') and subject_live_id=".$sid);
		if ($result){
			//记录行为日志
			$log_content = '<span style="color:red;">删除直播关联用户</span>[用户id：'.$del_uid.']';
			$this->manage_admin_log($log_content);
			showmessage(L('del_success'),'?m=subject_live&c=manage&a=user_list&id='.$sid);
		} else {
			showmessage(L('del_error'),HTTP_REFERER);
		}
	}
	
}